package com.markchu.modules.system.service.impl;

import com.markchu.common.data.PageSort;
import com.markchu.modules.system.domain.ActionLog;
import com.markchu.modules.system.domain.Upload;
import com.markchu.modules.system.repository.UploadRepository;
import com.markchu.modules.system.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author mark_chu
 * @date 2018/11/02
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UploadRepository uploadRepository;
    @Override
    public Page<Upload> getPageList(Example<Upload> example){
        PageRequest page = PageSort.pageRequest(20,PageSort.ORDER_BY_COLUMN_DEF, Sort.Direction.DESC);
        return uploadRepository.findAll(example,page);
    }

    @Override
    public Long counts(Example<Upload> example){
        return uploadRepository.count(example);
    }


    @Override
    public void updateUpload(Upload upload) {
        uploadRepository.updateUpload(upload.getRemark(),upload.getId());
    }
    @Override
    public void updateStatus(Long status,Long fileId){
        uploadRepository.updateStatus(status,fileId);
    }

    @Override
    public void updateAcIdByUpload(Long activityId, Long[] fileId) {
        if(!Objects.equals(fileId,null)){
            List<Long> filedIds = Arrays.asList(fileId);
            uploadRepository.updateAcIdByUpload(activityId,filedIds);
        }
    }

    @Override
    public Upload findById(Long id) {
        return uploadRepository.findById(id).orElse(null);
    }

    /**
     * 获取文件sha1值的记录
     */
    @Override
    public Upload getBySha1(String sha1) {
        return uploadRepository.findBySha1(sha1);
    }

    /**
     * 保存文件上传
     * @param upload 文件上传实体类
     */
    @Override
    public Upload save(Upload upload){
        return uploadRepository.save(upload);
    }
}

